// 引入pinia
import { defineStore } from "pinia";
// 引入接口函数
import { ReqGetAttrCategory1ListData, ReqGetAttrCategory2ListData, ReqGetAttrCategory3ListData } from "@/api/product/attr"
import type { categoryListInfoModel } from "@/api/product/model/attr"

// 创建store
export const useCategoryListStore = defineStore("categorySelectorList", {
  state: (): categoryListInfoModel => {
    return {
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      category1List: [],
      category2List: [],
      category3List: []
    }
  },
  actions: {
    //  1.发送请求 获取  一级下拉列表 数据
    async getCategory1List() {
      let result = await ReqGetAttrCategory1ListData()
      //  console.log(result)
      // 设置 一级下拉列表 数据
      this.category1List = result
    },
    // 2.发送请求 获取 二级下拉列表 数据
    async getCategory2List(category1Id: number) {
      let result = await ReqGetAttrCategory2ListData(category1Id)
      // console.log(result)
      // 设置 二级下拉列表 数据
      this.category2List = result
      this.category1Id = category1Id
      // 清空id
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
    },
    // 3.发送请求 获取 三级下拉列表 数据
    async getCategory3List(category2Id: number) {
      let result = await ReqGetAttrCategory3ListData(category2Id)
      // console.log(result)
      // 设置 三级下拉列表 数据
      this.category3List = result
      this.category2Id = category2Id
      // 清空id
      this.category3Id = undefined
    },
    // 4.给3级id赋值
    setCategory3Id(category3Id: number) {
      this.category3Id = category3Id
    }
  },
  getters: {

  }
})